import requests
import re
import json

# url = 'https://webfs.ali.kugou.com/202210301432/fef02d41656973f4f893ecd32cf5c190/KGTX/CLTX001/BFBDD3DF47727B701D4480EA36A8F73B.mp3'
# url_1 = 'https://webfs.ali.kugou.com/202210301432/fef02d41656973f4f893ecd32cf5c190/KGTX/CLTX001/bfbdd3df47727b701d4480ea36a8f73b.mp3'
#
# print(url)
# url = url.lower()
# print(url)
# print(url_1)

# ming = input('请输入歌名，歌手名：')

url = f'https://complexsearch.kugou.com/v2/search/song?callback=callback123&srcappid=2919&clientver=1000&clienttime=1667110822032&mid=fde6ad999df0e68d52b908031535d0ea&uuid=fde6ad999df0e68d52b908031535d0ea&dfid=4FIXNO2woASI3nqFUt1y5cF7&keyword=大鱼&page=1&pagesize=30&bitrate=0&isfuzzy=0&inputtype=0&platform=WebFilter&userid=0&iscorrection=1&privilege_filter=0&filter=10&token=&appid=1014&signature=4e2f09a293a5ec1605ca3b1df70d1e50'

headers = {
    'cookie': 'kg_mid=fde6ad999df0e68d52b908031535d0ea; kg_dfid=4FIXNO2woASI3nqFUt1y5cF7; kg_dfid_collect=d41d8cd98f00b204e9800998ecf8427e; Hm_lvt_aedee6983d4cfc62f509129360d6bb3d=1667110667; kg_mid_temp=fde6ad999df0e68d52b908031535d0ea; Hm_lpvt_aedee6983d4cfc62f509129360d6bb3d=1667110767'
    'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'
}
headers2 = {
    'cookie': 'kg_mid=fde6ad999df0e68d52b908031535d0ea; kg_dfid=4FIXNO2woASI3nqFUt1y5cF7; kg_dfid_collect=d41d8cd98f00b204e9800998ecf8427e; Hm_lvt_aedee6983d4cfc62f509129360d6bb3d=1667110667; Hm_lpvt_aedee6983d4cfc62f509129360d6bb3d=1667139190',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'
}
resp = requests.get(url=url, headers=headers)
j = resp.text[12:-2]
song_list = json.loads(j)['data']['lists']
# print(song_list)
for has in song_list:
    hash = has['FileHash']
    hash = hash.lower()
    name = has['FileName']

    # m_url = f'https://webfs.ali.kugou.com/202210301622/e9f127ee8d6a4eeb31bc43b55c84da22/KGTX/CLTX001/{hash}.mp3'
    # m_url = f'https://wwwapi.kugou.com//yy/index.php?r=play/getdata&hash={hash.get("FileHash")}'
    info_url = f'https://wwwapi.kugou.com//yy/index.php?r=play/getdata&hash={hash}'
    # print(info_url)
    info_resp = requests.get(url=info_url, headers=headers2)
    mp3_url = info_resp.json()['data']['play_url']
    title = mp3_url.split('/')[-1]
    print(name, title)
    try:
        resp_1 = requests.get(mp3_url, headers=headers).content
        with open(f'mp3/{name}{title}.mp3', mode='wb') as f:
            f.write(resp_1)
    except:
        pass



#   for i, s in enumerate(song_list):
#   print(f'{i+1}-----{s.get("FileName")}-------{s.get("FileHash")}')   # 格式化输出
#     info_resp = requests.get(url=info_url, headers=headers).content
    # with open(f'mp3/{name}.mp3', mode='wb') as f:
    #     f.write(m_resp)

# m_url = 'https://webfs.ali.kugou.com/202210301622/e9f127ee8d6a4eeb31bc43b55c84da22/KGTX/CLTX001/ee9f9b3954fa7e11d500e6f95b43fe17.mp3'
# m_resp = requests.get(url=m_url, headers=headers).content
# with open(f'{hash}', mode='wb')as f:
#     f.write(m_resp)













